package com.example.zhilierp.controller;

import com.example.zhilierp.entity.User;
import com.example.zhilierp.service.UserService;
import com.example.zhilierp.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户Controller类
 * 处理用户相关的HTTP请求
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    public Result<IPage<User>> getUserPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<User> page = new Page<>(pageNum, pageSize);
            IPage<User> userPage = userService.page(page);
            return Result.success(userPage);
        } catch (Exception e) {
            return Result.error("查询用户列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询用户详情
     */
    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        try {
            User user = userService.getById(id);
            if (user == null) {
                return Result.error("用户不存在");
            }
            return Result.success(user);
        } catch (Exception e) {
            return Result.error("查询用户详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增用户
     */
    @PostMapping
    public Result<Boolean> addUser(@RequestBody User user) {
        try {
            boolean saved = userService.save(user);
            if (saved) {
                return Result.success("新增用户成功", true);
            } else {
                return Result.error("新增用户失败");
            }
        } catch (Exception e) {
            return Result.error("新增用户失败：" + e.getMessage());
        }
    }
    
    /**
     * 修改用户
     */
    @PutMapping
    public Result<Boolean> updateUser(@RequestBody User user) {
        try {
            if (user.getId() == null) {
                return Result.error("用户ID不能为空");
            }
            boolean updated = userService.updateById(user);
            if (updated) {
                return Result.success("修改用户成功", true);
            } else {
                return Result.error("修改用户失败");
            }
        } catch (Exception e) {
            return Result.error("修改用户失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable Long id) {
        try {
            boolean deleted = userService.removeById(id);
            if (deleted) {
                return Result.success("删除用户成功", true);
            } else {
                return Result.error("删除用户失败");
            }
        } catch (Exception e) {
            return Result.error("删除用户失败：" + e.getMessage());
        }
    }
}